home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utildsk / memry374.lha / memory-device / source / makedisk.c < prev    next >
C/C++ Source or Header  |  1996-05-08  |  6KB  |  217 lines

  1. /*
  2. ** $VER: makedisk.c 1.0 (08 May 1996)
  3. **
  4. ** memory.device-based creating of a RAM disk
  5. **
  6. ** (C) Copyright 1996 Marius Gröger
  7. **     All Rights Reserved
  8. **
  9. ** $HISTORY:
  10. **
  11. ** 08 May 1996 : 001.000 :  created
  12. */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <dos/filehandler.h>
  17. #include <dos/dos.h>
  18. #include <libraries/expansion.h>
  19. #include <devices/input.h>
  20. #include <devices/inputevent.h>
  21. #include <clib/alib_stdio_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <pragmas/exec_pragmas.h>
  24. #include <clib/expansion_protos.h>
  25. #include <pragmas/expansion_pragmas.h>
  26. #include <clib/dos_protos.h>
  27. #include <pragmas/dos_pragmas.h>
  28. #include "compiler.h"
  29.  
  30. #define VERSION 37
  31. #define REVISION 1
  32. #define DATE "08.05.96"
  33. #define VERS "MakeDisk 37.1"
  34. #define VSTRING "MakeDisk 37.1 (08.05.96)"
  35. #define VERSTAG "\0$VER: MakeDisk 37.1 (08.05.96)"
  36. const STATIC UBYTE version[]=VERSTAG;
  37.  
  38. extern FAR void *DOSBase;
  39. struct Library *ExpansionBase;
  40.  
  41. extern void exit(LONG);
  42.  
  43. #include <string.h>
  44.  
  45. #define MYDOSTYPE ID_DOS_DISK
  46.  
  47. PRIVATE LONG parampkt[] = {
  48.    0,           /*XXX*/ /* DOS name */
  49.    (LONG)"memory.device",
  50.    0,
  51.    0,
  52.    11,                  /* Size of Environment vector */
  53.    0,           /*XXX*/ /* in longwords: standard value is 128 */
  54.    0,                   /* not used; must be 0 */
  55.    1,                   /* # of heads (surfaces). drive specific */
  56.    1,                   /* not used; must be 1 */
  57.    1,                   /* blocks per track. drive specific */
  58.    1,                   /* DOS reserved blocks at start of partition. */
  59.    0,                   /* DOS reserved blocks at end of partition */
  60.    0,                   /* usually 0 */
  61.    0,           /*XXX*/ /* starting cylinder. typically 0 */
  62.    0,           /*XXX*/ /* max cylinder. drive specific */
  63.    0,                   /* Initial # DOS of buffers.  */
  64.    0,                   /* type of mem to allocate for buffers */
  65.    (~0 >> 1),           /* Max number of bytes to transfer at a time */
  66.    ~1,                  /* Address Mask to block out certain memory */
  67.    0,                   /* Boot priority for autoboot */
  68.    MYDOSTYPE            /* ASCII (HEX) string showing filesystem type */
  69. };
  70.  
  71.  
  72. PRIVATE ULONG strntolong(STRPTR s, LONG *number, LONG n)
  73. {
  74.  
  75. #define IsDigit(c) ((c) >= '0' && (c) <= '9'))
  76. #define IsXDigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F'))
  77. #define ToUpper(c) (((c) >= 'a' && (c) <= 'f') ? ((c) & ~32) : (c))
  78.  
  79.    BOOL hex;
  80.    UBYTE c;
  81.    UWORD countdown;
  82.    ULONG preconv, rc;
  83.  
  84.    if ((n > 2) && (!strncmp(s, "0x", 2) || !strncmp(s, "0X", 2)))
  85.    {
  86.       hex = TRUE;
  87.       s += 2;
  88.       n -= 2;
  89.       preconv = 2;
  90.    }
  91.    else if ((n > 1) && (*s == '$'))
  92.    {
  93.       hex = TRUE;
  94.       ++s;
  95.       --n;
  96.       preconv = 1;
  97.    }
  98.    else
  99.    {
  100.       hex = FALSE;
  101.       preconv = 0;
  102.    }
  103.  
  104.    if (countdown=n)
  105.    {
  106.       LONG num;
  107.  
  108.       for(num=0; countdown && (*s != '\0'); ++s, --countdown)
  109.       {
  110.          c = ToUpper((ULONG)*s);
  111.          if (hex)
  112.          {
  113.             if (c >= '0' && c <= '9') num = num * 16 + c - '0';
  114.             else if (c >= 'A' && c <= 'F') num = num * 16 + c - 'A' + 10;
  115.             else break;
  116.          }
  117.          else
  118.          {
  119.             if (c >= '0' && c <= '9') num = num * 10 + c - '0';
  120.             else break;
  121.          }
  122.       }
  123.  
  124.       if (n - countdown)
  125.       {
  126.          *number = num;
  127.          rc = preconv + (n - countdown);
  128.       }
  129.       else rc = 0;
  130.    }
  131.    else rc = 0;
  132.  
  133.    return rc;
  134. }
  135.  
  136. void SAVEDS STDARGS main(void)
  137. {
  138.    struct RDArgs *rda;
  139.    struct {
  140.       UBYTE *disk;
  141.       UBYTE *name;
  142.       UBYTE *blocks;
  143.       ULONG getchip;
  144.    } opts;
  145.    APTR addr;
  146.    LONG blocks, size;
  147.    ULONG flags;
  148.    LONG rc = RETURN_FAIL;
  149.    PRIVATE UBYTE volname[34];
  150.    PRIVATE UBYTE dosname[34];
  151.  
  152.    if (ExpansionBase = OpenLibrary("expansion.library", 36))
  153.    {
  154.       memset(&opts, 0, sizeof(opts));
  155.  
  156.       if (rda = ReadArgs("DRIVE/A,NAME/K,B=BLOCKS/K/A,C=CHIP/S", (LONG*)&opts, NULL))
  157.       {
  158.          strntolong(opts.blocks, &blocks, (LONG)strlen(opts.blocks));
  159.          size = blocks * 512;
  160.  
  161.          flags = MEMF_CLEAR | MEMF_PUBLIC | (opts.getchip ? MEMF_CHIP : MEMF_ANY);
  162.  
  163.          if (addr = AllocVec(size+512, flags))
  164.          {
  165.             addr = (APTR)(((ULONG)addr + 511) & -512);
  166.  
  167.             parampkt[0] = (LONG)opts.disk;
  168.             parampkt[4+DE_SIZEBLOCK] = 512 / 4;
  169.             parampkt[4+DE_LOWCYL] = (ULONG)addr / 512;
  170.             parampkt[4+DE_UPPERCYL] = (ULONG)((UBYTE*)addr+size-512) / 512;
  171.  
  172.             if (AddBootNode(0, ADNF_STARTPROC, MakeDosNode(parampkt), NULL))
  173.             {
  174.                if (opts.name)
  175.                   strcpy(volname, opts.name);
  176.                else
  177.                   sprintf(volname, "$%lx", addr);
  178.                sprintf(dosname, "%s:", opts.disk);
  179.  
  180.                if (Inhibit(dosname, DOSTRUE))
  181.                {
  182.                   if (Format(dosname, volname, MYDOSTYPE))
  183.                      rc = RETURN_OK;
  184.                   else
  185.                      PrintFault(IoErr(), "Formatting of drive failed");
  186.  
  187.                   if (Inhibit(dosname, DOSFALSE) == DOSFALSE)
  188.                   {
  189.                      PrintFault(IoErr(), "Can't uninhibit drive");
  190.                   }
  191.                   else if (rc == RETURN_OK)
  192.                      Printf("Drive \"%s\" added.\n", dosname);
  193.                }
  194.                else
  195.                   PrintFault(IoErr(), "Can't inhibit drive");
  196.             }
  197.             else
  198.             {
  199.                PrintFault(IoErr(), "Can't add dos device");
  200.             }
  201.          }
  202.  
  203.          FreeArgs(rda);
  204.       }
  205.       else PrintFault(IoErr(), "Bad arguments");
  206.  
  207.       CloseLibrary(ExpansionBase);
  208.    }
  209.    else Printf("no expansion.library\n");
  210.  
  211.    exit(rc);
  212.  
  213.    return;
  214. }
  215. /*E*/
  216.  
  217.